package com.sevenstudio.adu.gasing.object;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.constants.PhysicsConstants;
import org.andengine.util.color.Color;

import com.badlogic.gdx.physics.box2d.Body;
import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.element.AText;
import com.sevenstudio.adu.gasing.engine.Anchor;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;

public class Enemy extends Gasing
{
	Gasing			mc;
	TimerHandler	timerAttack;
	boolean			isPlay = true;
	AText txt_cpu;
//	ASprite spr_api;
	
	public Enemy(GameEngine gameEngine, GasingElement[] arrayGasing, int index,
			PhysicsWorld world, Gasing mc)
	{
		super(gameEngine, arrayGasing, index, world);
		this.mc = mc;
		
		txt_cpu = new AText(engine, iGameDefines.TEXT_ENEMY, 0, engine.gameFont[6]);
		txt_cpu.theText().setColor(Color.RED);
		
		txt_cpu.SetPos(sprite.getX(), sprite.getY() - Utils.GetRatioHeight(20));
//		spr_api = new ASprite(engine, iGameDefines.SPRITE_EFFECT, 0);
		
		
		attack();
	}
	
	private void attack()
	{
		timerAttack = new TimerHandler(2, true, new ITimerCallback()
		{
			public void onTimePassed(TimerHandler pTimerHandler)
			{
				float dist_x = mc.getBody().getPosition().x
						- body.getPosition().x;
				
				float dist_y = mc.getBody().getPosition().y
						- body.getPosition().y;
				
				body.setLinearVelocity(getSpeedX(dist_x, dist_y),
						getSpeedY(dist_x, dist_y));
				
//				spr_api.theSprite().setVisible(true);
			}
		});
		
		
	}
	
	public boolean getplay()
	{
		return isPlay;
	}
	
	@Override
	public void onUpdate()
	{
		super.onUpdate();
		
		if (play)
		{
			if (isPlay)
			{
				isPlay = false;
				play = false;
				sprite.registerUpdateHandler(timerAttack);
			}
		}
				
		txt_cpu.SetPos(sprite.getX(), sprite.getY() - Utils.GetRatioHeight(20));
//		spr_api.SetPos(sprite.getX() + Utils.GetRatio(35), sprite.getY() + Utils.GetRatioHeight(20));
	}
	
	@Override
	public void reset()
	{
		super.reset();
		
		sprite.unregisterUpdateHandler(timerAttack);
		isPlay = true;
		body.setTransform((Utils.GetAnchorX(Utils.GetRatio(gasing.getW()), Anchor.CENTER_RIGHT) - Utils.GetRatio(200)) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
				(Utils.GetAnchorY(Utils.GetRatio(gasing.getH()), Anchor.CENTER_RIGHT) + Utils.GetRatio(0)) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, 0);
	
	}
	
	public Body getBody()
	{
		return body;
	}
	
	public AText getText()
	{
		return txt_cpu;
	}
	
//	public ASprite getEffect()
//	{
//		return spr_api;
//	}
	
}
